pragma circom 2.0.0;

include "comparators.circom";
include "bitify.circom";
include "gates.circom";
include "./id_card.circom";
include "./sub_circuit.circom";

template AgeRangeCheck() {
    // 公开输入
    signal input birthYear;
    signal input minAge;
    signal input maxAge;
    signal input currentYear;
    
    // 私有输入
    signal input idCardPrefix;
    signal input idCardSuffix;
    
    // 输出
    signal output valid;
    
    // 1. 验证身份证格式
    component idCheck = IDCardCheck();
    idCheck.prefix <== idCardPrefix;
    idCheck.suffix <== idCardSuffix;
    idCheck.birthYear <== birthYear;
    
    // 2. 计算年龄
    component age = SubCircuit(32);
    age.a <== currentYear;
    age.b <== birthYear;
    
    // 3. 验证年龄范围
    component minCheck = GreaterEqThan(32);
    minCheck.in[0] <== age.out;
    minCheck.in[1] <== minAge;
    
    component maxCheck = LessThan(32);
    maxCheck.in[0] <== age.out;
    maxCheck.in[1] <== maxAge;
    
    // 4. 使用AND门组合结果
    component and1 = AND();
    and1.a <== idCheck.valid;
    and1.b <== minCheck.out;
    
    component and2 = AND();
    and2.a <== and1.out;
    and2.b <== maxCheck.out;
    
    valid <== and2.out;
}

component main = AgeRangeCheck();